package com.helloae.chap05;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IObjectClass;
import com.esri.arcgis.geodatabase.IObjectClassProxy;
import com.esri.arcgis.geodatabase.IRelQueryTable;
import com.esri.arcgis.geodatabase.IRelationshipClass;
import com.esri.arcgis.geodatabase.ITable;
import com.esri.arcgis.geodatabase.MemoryRelationshipClassFactory;
import com.esri.arcgis.geodatabase.RelQueryTableFactory;
import com.esri.arcgis.geodatabase.esriJoinType;
import com.esri.arcgis.geodatabase.esriRelCardinality;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 一个shp文件join关联两个dbf文件
 * 示例章节：5.6.2
 * @author zhushijie
 *
 */
public class JoinMultipleTables {

	public static void main(String[] args) throws UnknownHostException, IOException {
		// 初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
		
		//使用ShapefileWorkspaceFactory.openFromFile的方法创建一个Workspace
		ShapefileWorkspaceFactory pWF = new ShapefileWorkspaceFactory();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWF
				.openFromFile(ecliseWorkspace + "HelloAE" + File.separator
						+ "DataSet" + File.separator + "chap05", 0);
		//加载图层
		IFeatureClass pFeatureClass = pFeatureWorkspace
				.openFeatureClass("idcounty4");
		
		//打开用于join的数据集population，即表
		ITable pFromTable1 = pFeatureWorkspace.openTable("change");
		//将ITable转型成IObjectClass，否则程序不认该参数
		IObjectClass originForeignClass1 =new IObjectClassProxy(pFromTable1);
		
		//打开用于join的数据集population，即表
		ITable pFromTable2 = pFeatureWorkspace.openTable("population");
		//将ITable转型成IObjectClass，否则程序不认该参数
		IObjectClass originForeignClass2 =new IObjectClassProxy(pFromTable2);
		
		MemoryRelationshipClassFactory pMemRelFact = new MemoryRelationshipClassFactory();
		IRelationshipClass pRelClass1 = pMemRelFact.open("Table1ToLayer", pFeatureClass, "co_name", originForeignClass1, "co_name", "forwardPathLabel", "backwardPathLabel", esriRelCardinality.esriRelCardinalityOneToOne);
		
		RelQueryTableFactory pRelQueryTableFact = new RelQueryTableFactory();
		//因为需要二次join，IRelationshipClass并不能直接转换为IObjectClass，因此这里需要先转换为IRelQueryTable
		IRelQueryTable pRelQueryTab1 = pRelQueryTableFact.open(pRelClass1, true, null, null, "", true, true);
		//将ITable转型成IObjectClass，否则程序不认该参数
		IObjectClass originForeignClass3 =new IObjectClassProxy(pRelQueryTab1);
		
		//这里的idcounty4.co_name也可以写成change.co_name，当然这就和关联的第一张表有关，如果先关联的是population，那这里就需要写population.co_name
		IRelationshipClass pRelClass2 = pMemRelFact.open("Table1ToLayer", originForeignClass3, "idcounty4.co_name", originForeignClass2, "co_name", "forwardPathLabel", "backwardPathLabel", esriRelCardinality.esriRelCardinalityOneToOne);
		
//		IRelQueryTable pRelQueryTab2 = pRelQueryTableFact.open(pRelClass2, true, null, null, "", true, true);
		
		FeatureLayer pFeatureLayer = new FeatureLayer();
		pFeatureLayer.setFeatureClassByRef(pFeatureClass);
		System.out.println("join以前的字段数量为："+pFeatureLayer.getFieldCount());
		
		pFeatureLayer.displayRelationshipClass(pRelClass2, esriJoinType.esriLeftInnerJoin);
		System.out.println("join以后的字段数量为："+pFeatureLayer.getFieldCount());
		
		ao.shutdown();
		System.out.println("Congratulations!");
	}

}
